from selenium.webdriver import ActionChains
from hyrobot.common import STEP, CHECK_POINT, INFO
from lib.webui_local_mysql import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from pymysql_connect import *
from creat_table import *



class c73250b:
    # this need create pro...num table first
    # name = '产品管理员录入显示产品数量相关信息'  # 测试用例名字
    # may用到文件读入产品管理员账号
    cases = [
        ('c73250b1', 'zhangsan', '123456'),
        ('c73250b2', 'lijunyi', '123456'),
        # ('c73250b3', 'zhang13', '123456'),
        ('c73250b5', 'zhang32', '123456'),
        ('c73250b6', 'lvdelin', '123456'),
        ('c73250b7', 'zhang86', '123456'),
        ('c73250b8', 'zhang85', '123456'),
        ('c73250b10', 'liuyuze', '123456'),
        ('c73250b11', 'wangxinyao', '123456'),
        ('c73250b12', 'shihao', '123456'),
        ('c73250b13', 'zhang83', '123456'),
    ]

    def setup(self):
        STEP(1, '登陆网站')
        wd = open_browser()

    # 清除方法
    def teardown(self):
        wd = get_global_webdriver()
        wd.quit()
    # 测试用例步骤
    def teststeps(self, para_index):
        if para_index == 0:
            create_product_num_table()

        # 取出参数
        username, password = self.cases[para_index][1:3]
        # 下面写登录流程
        driver = get_global_webdriver()
        driver.get("http://172.17.66.19:8080/")
        driver.find_element(By.NAME, "username").send_keys(username)
        driver.find_element(By.NAME, "password").send_keys(password)
        driver.find_element(By.CSS_SELECTOR, ".el-button").click()
        time.sleep(1)
        extending_button = driver.find_element_by_id("hamburger-container")
        try:
            driver.find_element(By.CLASS_NAME, "el-tooltip")
            extending_button.click()
        except:
            INFO("左侧栏没有隐藏")
        # click product manage
        time.sleep(1)
        product_manage_word2 = driver.find_element(By.XPATH,
                                                   "/html/body/div/div/div[1]/ul/div[3]/a/li/span"
                                                   )
        product_manage_word2.click()
        time.sleep(1)
        top_title2 = driver.find_element(By.XPATH,
                                         "/html/body/div/div/div[2]/div/div/div[2]/span/span[2]/span[1]/span"
                                         ).text
        CHECK_POINT("上方第3个标题对应", "产品管理" == top_title2)

        STEP(2, "读取表格中产品名称，创建人，显示信息到数据库")
        global_insert_list = []
        first_page_list = get_current_page_insert_list(driver)
        global_insert_list.extend(first_page_list)
        INFO(global_insert_list)
        prev_button = driver.find_element_by_class_name("btn-next")
        if not prev_button.get_attribute("disabled"):
            INFO(prev_button.get_attribute("disabled"))
            while prev_button.get_attribute("disabled") == None:
                prev_button.click()
                time.sleep(2)
                current_insert_list = get_current_page_insert_list(driver)
                global_insert_list.extend(current_insert_list)
                time.sleep(1)
                prev_button = driver.find_element_by_class_name("btn-next")
                if prev_button.get_attribute("disabled") == "disabled":
                    break

        INFO(global_insert_list)
        db = connect()
        # 使用cursor()方法获取操作游标
        INFO(3)
        cursor = db.cursor()


        # SQL 插入语句
        sql = "INSERT INTO product_num(product_name,version,creater,create_time,update_time,is_display) VALUES (%s,%s,%s,%s,%s,%s)"
        try:
            # 执行sql语句
            cursor.executemany(sql,global_insert_list)
            # 提交到数据库执行
            INFO(4)
            db.commit()
            INFO(5)
            cursor.close()
        except:
            # 如果发生错误则回滚
            db.rollback()
            INFO(6)

        # 关闭数据库连接
        db.close()
        # driver.quit()
